﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLogic.DTO;
using BusinessLogic.Repository_Logic.Interfaces;
using TestSystem.Domain.Entities.Position;
using TestSystem.Domain.Abstract;
using TestSystem.Domain.Repositories;

namespace BusinessLogic.Repository_Logic.Concentrate
{
    public class PositionRepositoryLogic : IPositionRepositoryLogic
    {
        private readonly IPositionRepository _positionRepository = new PositionRepository();
        private readonly IQuestionRepository _questionRepository = new QuestionRepository();
        private readonly ICategoryRepository _categoryRepository = new CategoriesRepository();

        public PositionDTO GetPositionByID(int positionID)
        {
            throw new NotImplementedException();
        }

        public PositionDTO GetEmptyPositionDTO()
        {
            PositionDTO positionDto = new PositionDTO();
            positionDto.Categories = _categoryRepository.Categories.ToList();
            positionDto.Difficulties = _questionRepository.Difficulties.ToList();
            positionDto.QuestionTypes = _questionRepository.QuestionTypes.ToList();
            positionDto.CategoryToPosition = new List<CategoryToPosition>();
            positionDto.QuestionDifficultyToPosition = new List<QuestionDifficultyToPosition>();
            positionDto.QuestionTypeToPosition = new List<QuestionTypeToPosition>();
            positionDto.Position = new Position();

            return positionDto;
        }

        public List<PositionDTO> GetAllPositions()
        {
            List<PositionDTO> positions = new List<PositionDTO>();
            foreach (var pos in _positionRepository.Positions.ToList())
            {
                positions.Add(new PositionDTO()
                                  {
                                      Position = pos,
                                      QuestionDifficultyToPosition = _positionRepository.QuestionDifficultyToPositions.Where(p =>p.PositionID == pos.PositionID).ToList(),
                                      CategoryToPosition = _positionRepository.CategoryToPositions.Where(p =>p.PositionID == pos.PositionID).ToList(),
                                      QuestionTypeToPosition = _positionRepository.QuestionTypeToPositions.Where(p =>p.PositionID == pos.PositionID).ToList()
                                  });
            }
            return positions;
        }

        public void SavePosition(PositionDTO positionDto)
        {
            _positionRepository.addPosition(positionDto.Position);
            var position =
                _positionRepository.Positions.First(pos => pos.PositionName.Equals(positionDto.Position.PositionName));

            positionDto.CategoryToPosition.ForEach(ctp => ctp.PositionID = position.PositionID);
            positionDto.QuestionDifficultyToPosition.ForEach(qdtp => qdtp.PositionID = position.PositionID);
            positionDto.QuestionTypeToPosition.ForEach(qttp => qttp.PositionID = position.PositionID);

            positionDto.CategoryToPosition.ForEach(ctp => _positionRepository.addCategoryToPosition(ctp));
            positionDto.QuestionDifficultyToPosition.ForEach(qdtp => _positionRepository.addQuestionDifficultyToPosition(qdtp));
            positionDto.QuestionTypeToPosition.ForEach(qttp => _positionRepository.addQuestionTypeToPosition(qttp));
        }

        public void RemovePosition(int positionId)
        {
            var position = _positionRepository.Positions.First(pos => pos.PositionID.Equals(positionId));

            var CategoriesToPosition =
                _positionRepository.CategoryToPositions.Where(pos => pos.PositionID.Equals(position.PositionID)).ToList();
            CategoriesToPosition.ForEach(cat => _positionRepository.RemoveCategoryToPosition(cat));

            var QuestionDifficultiesToPosition =
               _positionRepository.QuestionDifficultyToPositions.Where(pos => pos.PositionID.Equals(position.PositionID)).ToList();
            QuestionDifficultiesToPosition.ForEach(diff => _positionRepository.RemovedQuestionDifficultyToPosition(diff));

            var QuestionTypesToPosition =
               _positionRepository.QuestionTypeToPositions.Where(pos => pos.PositionID.Equals(position.PositionID)).ToList();
            QuestionTypesToPosition.ForEach(type => _positionRepository.RemoveQuestionTypeToPosition(type));

            _positionRepository.RemovePosition(position);
        }

        public int PositionAmount()
        {
            return _positionRepository.Positions.Count();
        }

        public void UpdateRecruitmentStatus(int positionId)
        {
            var position = _positionRepository.Positions.First(pos => pos.PositionID.Equals(positionId));

            if (position.Recruitment)
                position.Recruitment = false;
            else
                position.Recruitment = true;

            _positionRepository.SaveChanges();
        }

        public List<Position> GetPositionsToRecruitment()
        {
            return _positionRepository.Positions.Where(position => position.Recruitment).ToList();
        }
    }
}
